<?php
/**
 * Autor:   Miguel A. Zilli
 * Email:   miguelzilli@gmail.com
 */

class RecoinstalacionesController extends AppController{
    var $name='Recoinstalaciones';
    var $uses = array('Recoinstalacion','Revision','Instalacionestipo','Atributo','Atributosxrecomendacion');
    var $helpers=array('Ajax','Javascript');

    function agregar($id_revision){
        if (!empty($this->data)){
            $this->data['Recoinstalacion']['revision_id']=$id_revision;
            if ($this->Recoinstalacion->save($this->data)){
                $id_recoinst=$this->Recoinstalacion->getID();
                foreach($this->data['Atributosxrecomendacion'] as $key=>$data){
                    $this->data['Atributosxrecomendacion'][$key]['recoinstalacion_id']=$id_recoinst;
                    $this->Atributosxrecomendacion->save($this->data['Atributosxrecomendacion'][$key]);
                    $this->Atributosxrecomendacion->create();
                }
                $this->Session->setFlash('Operacion realizada correctamente.');
                $this->redirect('/revisiones/ver/'.$id_revision);
            }else{
                $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            }
        }
        $this->set('revision',$this->Revision->find('first',array('conditions'=>'Revision.id='.$id_revision)));
        $this->set('instalacionestipos',$this->Instalacionestipo->find('list',array(
                    'fields'=>'desc_tipo_inst',
                    'order'=>'desc_tipo_inst ASC'
                )));
        $primeraInst=$this->Instalacionestipo->find('first',array('order'=>'desc_tipo_inst ASC'));
        $this->set('atributos',$this->Atributo->find('all',array(
                    'conditions'=>'Atributo.instalacionestipo_id='.$primeraInst['Instalacionestipo']['id'],
                    'order'=>'nombre_atr ASC'
                )));
    }

    function editar($id=null){
        if (!empty($this->data)){
            if ($this->Recoinstalacion->save($this->data)){
                if(!empty($this->data['Atributosxrecomendacion'])){
                    foreach($this->data['Atributosxrecomendacion'] as $key=>$data){
                        $this->Atributosxrecomendacion->create($this->data['Atributosxrecomendacion'][$key]);
                        $this->Atributosxrecomendacion->save($this->data['Atributosxrecomendacion'][$key]);
                    }
                }
                $this->Session->setFlash('Operacion realizada correctamente.');
                $this->redirect('/revisiones/ver/'.$this->data['Recoinstalacion']['revision_id']);
            }else{
                $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            }
        }
        if (empty($this->data)){
            $this->data=$this->Recoinstalacion->read(null,$id);
        }
        $this->data+=$this->Atributosxrecomendacion->find('all',array(
                'conditions'=>'Atributosxrecomendacion.recoinstalacion_id='.$id,
                'order'=>'Atributo.nombre_atr ASC'
            ));
    }

    function eliminar($id=null){
        $id_revision=$this->Recoinstalacion->field('Recoinstalacion.revision_id',array('Recoinstalacion.id'=>$id));
        if($this->Recoinstalacion->del($id)){
            $this->Session->setFlash('Operacion realizada correctamente.');
            $this->redirect('/revisiones/ver/'.$id_revision);
        }else{
            $this->Session->setFlash('Error: No se pudo realizar la operacion.');
            $this->redirect('/revisiones/ver/'.$id_revision);
        }
    }

    function update_atributosxreco(){
        Configure::write("debug",0);
        if (!empty($this->data['Recoinstalacion']['instalacionestipo_id'])){
            $id_tipoInst= $this->data['Recoinstalacion']['instalacionestipo_id'];
            $atributos=$this->Atributo->find('all',array(
                    'conditions'=>'Atributo.instalacionestipo_id='.$id_tipoInst,
                    'order'=>'nombre_atr ASC'
                ));
        }else{
            $atributos=false;
        }
        $this->set('atributos', $atributos);
        $this->render('/elements/update_atributosxreco', 'ajax');
    }
}
?>